//
//  BTBworld.m
//  my-BTB
//
//  Created by QuickSoluion on 9/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BTBworld.h"
#import "Weapon.h"
#import "DataHelper.h"
#import "dataBridge.h"
#import "Bullet.h"
#import "bb.h"
#import "bbx.h"
#import "bbt.h"

@implementation BTBworld
@synthesize _SMG=_spaceManager;

int iNumberPiece=0;

-(id)initWithMap:(int)numberMap
{
	if( (self=[super init])) {
		iNumberPiece=0;
		
		//_saveStructBridge=[[NSMutableArray alloc] init];
		// enable
		[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
		_spaceManager = [[SpaceManagerCocos2d alloc] init];
		_spaceManager.constantDt=1.0/60.0; //1.0/55.0f; // speech of gameplay //1.60	
		_spaceManager.gravity= cpv(0, -150.0);
		/*[_spaceManager addCollisionCallbackBetweenType:kBridgeColition otherType:kBridgeColition target:self 
											  selector:@selector(Handle_BridgeColision:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];*/
		
		
		[_spaceManager addCollisionCallbackBetweenType:kBomColition otherType:kStreetLine target:self 
											  selector:@selector(Handle_BombColision:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];
		
		[_spaceManager addCollisionCallbackBetweenType:kBomColition otherType:kChanCauColition target:self 
											  selector:@selector(Handle_BombColision:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];
		
		[_spaceManager addCollisionCallbackBetweenType:kBomColition otherType:kGroundColition target:self 
											  selector:@selector(Handle_BombColision:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];  ///// between bomb and ground
		
		
		[_spaceManager addCollisionCallbackBetweenType:kBomColition otherType:kBridgeColition target:self 
											  selector:@selector(Handle_BombColision:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];
		
		
		//// hieu ung khoilua
		
		[_spaceManager addCollisionCallbackBetweenType:kBridgeColition otherType:kGroundColition target:self 
											  selector:@selector(Handle_BridgeCGround:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];
		
		[_spaceManager addCollisionCallbackBetweenType:kStreetLine otherType:kGroundColition target:self 
											  selector:@selector(Handle_BridgeCGround:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];
		
		///////
		
		[_spaceManager addCollisionCallbackBetweenType:kBridgeColition otherType:kTrucCoDinh target:self 
											  selector:@selector(Handle_BridgeTrucCoDinh:arbiter:space:)
											   moments:COLLISION_POSTSOLVE,nil];
		
		[_spaceManager addCollisionCallbackBetweenType:kCar otherType:kWinColition target:self 
											  selector:@selector(Handle_winGame:arbiter:space:)
											   ];//COLLISION_POSTSOLVE
		
		
		
		/// roi xuong cau
		{
			//duong tren cau
			/*[_spaceManager addCollisionCallbackBetweenType:kStreetLine otherType:kWaterColition target:self 
												  selector:@selector(Handle_fallWater:arbiter:space:)];*/
			//thanh cau
			/*[_spaceManager addCollisionCallbackBetweenType:kBridgeColition otherType:kWaterColition target:self 
												  selector:@selector(Handle_fallWater:arbiter:space:)];*/
			
			///xe
			/*[_spaceManager addCollisionCallbackBetweenType:kCar otherType:kWaterColition target:self 
												  selector:@selector(Handle_fallWater:arbiter:space:)];*/
			
			
		}
		/// ten lua no
		{
			
			[_spaceManager addCollisionCallbackBetweenType:kCar otherType:kMissleColition target:self 
											  selector:@selector(Handle_tenlua:arbiter:space:)];
			[_spaceManager addCollisionCallbackBetweenType:kBridgeColition otherType:kMissleColition target:self 
												  selector:@selector(Handle_tenlua:arbiter:space:)];
			[_spaceManager addCollisionCallbackBetweenType:kStreetLine otherType:kMissleColition target:self 
												  selector:@selector(Handle_tenlua:arbiter:space:)];
			[_spaceManager addCollisionCallbackBetweenType:kMissleColition otherType:kMissleColition target:self 
												  selector:@selector(Handle_tenlua:arbiter:space:)];
			[_spaceManager addCollisionCallbackBetweenType:kGroundColition otherType:kMissleColition target:self 
												  selector:@selector(Handle_tenlua:arbiter:space:)];
		}
		
		
		
		
		[self addThreeWallInGame];
		//[self SetupCarAt:ccp(100,100)];///100,100
		[self initParticleWeather];
	}
	
	return self;
}
-(BOOL)Handle_tenlua:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	CP_ARBITER_GET_SHAPES(arb, otherShape,tenluaShape);
	
	
	[(Bullet*)tenluaShape->data Destroy:_spaceManager:otherShape->body];	
	//[(cpCCSprite*)otherShape->data shape]->body->v  =cpvzero;
	//[(cpCCSprite*)bombShape->data shape]->body->v  =cpvzero;
	return YES;
	
}


-(void)getTempGamePlayer:(CCLayer*)_Gamelayer
{
	TempGamePlayer=_Gamelayer;
}




-(BOOL)Handle_winGame:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	CP_ARBITER_GET_SHAPES(arb, carbShape, winShape);
	
	NSLog(@"win game");
	[_spaceManager stop];
	//[(cpCCSprite*)otherShape->data shape]->body->v  =cpvzero;
	//[(cpCCSprite*)bombShape->data shape]->body->v  =cpvzero;
	
	[self initEntityByClassOfworld:[bb class]:ccp(450,200):1.0:NO];
	[self initEntityByClassOfworld:[bb class]:ccp(400,140):1.0:NO];
	[self initEntityByClassOfworld:[bbt class]:ccp(470,140):1.0:NO];
	[self initEntityByClassOfworld:[bbx class]:ccp(480,140):1.0:NO];
	
	
	
	CCLabelTTF* lb=[CCLabelTTF labelWithString:@"YOU WIN" fontName:@"Arial" fontSize:22];
	[lb setAnchorPoint:ccp(0,0)];
	[lb setColor:ccRED];
	lb.position=ccp(400,120);
	[self addChild:lb z:2000];
	
				   
	
	return YES;
	
}
-(Character*)initEntityByClassOfworld:(Class)tempC:(CGPoint)p:(float)fScale:(BOOL)yes_noScale
{
	Character* _itemClass=[[[tempC alloc] initWithParent:self] autorelease];
	
	if(yes_noScale==YES)
	{
		[_itemClass setScale:fScale];
	}
	_itemClass.position = p	;				
	id Action = [_itemClass createAnimation];
	[_itemClass runAction:Action];
	
	return _itemClass;
	
}




-(BOOL)Handle_fallWater:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	/*if(moment == COLLISION_BEGIN)
	{
		CP_ARBITER_GET_SHAPES(arb, fallWaterShape, waterShape);
	
		NSLog(@"fall water");
	
	//[(cpCCSprite*)fallWaterShape->data removeFromParentAndCleanup:TRUE];
	//[(cpCCSprite*)fallWaterShape->data release];
	
		cpShapeNode *removeShapeNode = (cpShapeNode*)(fallWaterShape->data);
	
		[self removeChild:removeShapeNode cleanup:YES];
		removeShapeNode=nil;
	//[(cpCCSprite*)fallWaterShape->data  shape]=nil;
	//[fallWaterShape release];
	//fallWaterShape=nil;
	}*/
	return YES;
}



-(void)initParticleWeather
{
	ParticleAnimation * temp =[[ParticleAnimation alloc] init];
	[temp initextra];
	emitter=[temp particleEmiiter_extra:ccp(250,250) afterDelay:1];//ccp(400,700)		
	[self addChild:emitter z:100];		
	
	[temp release];
}


-(void)initParticleSmokeAtPosition:(CGPoint)p
{
	/*ParticleAnimation * temp =[[ParticleAnimation alloc] init];
	[temp initParticleEmitter_Smoke];
	CCParticleSystem *emitter=[temp particleEmiiterAt_Smoke:p afterDelay:1];//ccp(400,700)
	[self addChild:emitter z:100000];
	[temp release];*/
	
	CCParticleSmoke *smoke = [[[CCParticleSmoke alloc] initWithTotalParticles:40] autorelease];
	smoke.texture= [[CCTextureCache sharedTextureCache] addImage: @"stars.png"];
	
	smoke.position = p;
	smoke.duration = .5;
	smoke.speed = 40;
	smoke.autoRemoveOnFinish = YES;
	smoke.blendAdditive = YES;//NO
	
	 [self addChild:smoke];
	
}

-(void)initParticleFireAtPosition:(CGPoint)p
{
	ParticleAnimation * temp =[[ParticleAnimation alloc] init];
	[temp initParticleEmitter_Fire];
	emitter=[temp particleEmiiterAt_Fire:p afterDelay:1];//ccp(400,700)
	[self addChild:emitter z:100000];
	[temp release];
}


-(void)StartSpysic
{
	[_spaceManager start];
	
}
int  isAnimation=0;
-(BOOL)Handle_BridgeCGround:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	CP_ARBITER_GET_SHAPES(arb, bombShape, otherShape);
	
	if(isAnimation>1)
	{
		return TRUE;
	}
	isAnimation++;
	
	//[self initParticleSmokeAtPosition:[(cpCCSprite*)bombShape->data position]];
	
	//[(cpCCSprite*)otherShape->data shape]->body->v  =cpvzero;
	//[(cpCCSprite*)bombShape->data shape]->body->v  =cpvzero;
	return YES;
	
}

-(BOOL)Handle_BridgeTrucCoDinh:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	CP_ARBITER_GET_SHAPES(arb, bombShape, otherShape);
	[(cpCCSprite*)otherShape->data shape]->body->v  =cpvzero;
	[(cpCCSprite*)bombShape->data shape]->body->v  =cpvzero;
	return YES;
	
}

-(BOOL)Handle_BombColision:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	
	CP_ARBITER_GET_SHAPES(arb, bombShape, otherShape);	
	[(Weapon*)bombShape->data startCountDown:_spaceManager:otherShape->body];	
	//[_spaceManager removeAndFreeConstraintsOnBody:otherShape->body];///------> xai ham nay good
	return YES;
}
-(BOOL)Handle_BridgeColision:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
	
	CP_ARBITER_GET_SHAPES(arb, bombShape, otherShape);
	
	[(cpCCSprite*)otherShape->data shape]->body->v  =cpvzero;
	[(cpCCSprite*)bombShape->data shape]->body->v  =cpvzero;
	
	//cpBodyResetForces(otherShape->body);
	//cpBodyResetForces(bombShape->body);
	//[otherShape->body resetForces];
	//[_spaceManager removeAndFreeConstraintsOnBody:otherShape->body];
	return YES;
	//CP_ARBITER_GET_SHAPES(arb, bombShape, otherShape);	
	//[(Weapon*)bombShape->data startCountDown:_spaceManager:otherShape->body];	
	/////------> xai ham nay good
	
}

-(void)SetupBomb:(CGPoint)P
{
	NSLog(@"%8.3f- %8.3f",P.x,P.y);
	Weapon*bomb = [Weapon bombWithGame:self];
	bomb.position = P;//ccp(250, 210);
	[self addChild:bomb z:5];
	//[_bombs addObject:bomb];
	[_spaceManager morphShapeToActive:bomb.shape mass:30];
	
	[bomb startCountDown:_spaceManager :nil];
}
-(void)settupMissle:(CGPoint)P
{
	Bullet*missle = [Bullet BulletWithGame:self];
	missle.position = P;//ccp(250, 210);
	[self addChild:missle z:5];	
	[_spaceManager morphShapeToActive:missle.shape mass:1000];//30
	
	float alpha = -(45* M_PI / 180);
	CGPoint vector = CGPointMake(100.0 * cos(alpha), 100.0 * sin(alpha));
	
	[missle applyImpulse:cpvmult(vector, 340)];
}

-(void)SetupCarAt:(CGPoint)P
{
	NSLog(@"%8.3f- %8.3f",P.x,P.y);	
	[self createImageEntityAt:P width:49 height:15 fileName:@"xehoi.png" kColitionType:kCar Mass:3000];///10000///9000
}




-(void)addThreeWallInGame
{
	iCountData= [DataHelper countData:@"map1"];
	NSLog(@"count=%d",iCountData);
	[_spaceManager addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpvzero]; /// elasticity =1.0	(dan hoi)  Friction:1.0(ma sat)
	
	// ve ground
	{
		[self setupWallbyColor:0 andColition:kWaterColition from:cpv(0,2) to:cpv(650,2)];///kGroundColition//1
	}
	//// ho tro chan cau
	{
		[self setupWallbyColor:0 andColition:kGroundColition from:cpv(0,2) to:cpv(0,60)];//5	
		[self setupWallbyColor:0 andColition:kGroundColition from:cpv(35,2) to:cpv(35,40)];	//5
		[self setupWallbyColor:0 andColition:kGroundColition from:cpv(280,2) to:cpv(280,30)];//5	
		[self setupWallbyColor:0 andColition:kGroundColition from:cpv(322,2) to:cpv(322,40)];	//5
	}
	
	// diem noi
	
	cpShape *lineLeft1=[self setupWallbyColor:0 andColition:kGroundColition from:cpv(0,50) to:cpv(0,55)];
	cpShape *lineLeft2=[self setupWallbyColor:0 andColition:kGroundColition from:cpv(0,85) to:cpv(0,90)];
	cpShape *lineLeft3=[self setupWallbyColor:0 andColition:kGroundColition from:cpv(-10,103) to:cpv(-10,108)];
	cpShape *lineRight1=[self setupWallbyColor:0 andColition:kGroundColition from:cpv(322,50) to:cpv(322,55)];
	cpShape *lineRight2=[self setupWallbyColor:0 andColition:kGroundColition from:cpv(322,85) to:cpv(322,90)];
	cpShape *lineRight3=[self setupWallbyColor:0 andColition:kGroundColition from:cpv(322,103) to:cpv(322,108)];
		
	
	
		
	// ve truc co dinh (ham luc)
	{
		[self setupWallbyColor:0 andColition:kTrucCoDinh from:cpv(5,117) to:cpv(10,117)];//5
		[self setupWallbyColor:0 andColition:kTrucCoDinh from:cpv(300,117) to:cpv(305,117)];//5
	}
	/////// ve vung dat
	{
		[self setupWallbyColor:0 andColition:kGroundColition from:cpv(323,0) to:cpv(323,110)];//4
		[self setupWallbyColor:0 andColition:kGroundColition from:cpv(323,110) to:cpv(480,110)];//4
	}
	// ve garage
	{
		[self setupWallbyColor:0 andColition:kWinColition from:cpv(480,110) to:cpv(480,160)];//2
		[self setupWallbyColor:0 andColition:kWinColition from:cpv(480,160) to:cpv(485,162)];//2
		[self setupWallbyColor:0 andColition:kWinColition from:cpv(485,162) to:cpv(485,196)];//2
		[self setupWallbyColor:0 andColition:kWinColition from:cpv(485,196) to:cpv(750,196)];//2
	}
	
	
	
	//iCountData
	for (int i=0; i<iCountData; i++)
	{
		int tempColition=0;
		int Mass =25000;////50000
		if(i<10)
		{
			tempColition=kStreetLine;
			Mass =25000;
		}
		else if(i==23||i==24||i==25)
		{
			tempColition=kChanCauColition;
			Mass =40000;
		}
		else {
			tempColition=kBridgeColition;
			Mass=30000;
		}

		
		dataBridge *item=[[DataHelper loadDataBridgeAt:@"map1":i] retain];
		[self createImageEntityAt:item._cgPosition width:item._width height:item._height fileName:item._strName kColitionType:tempColition Mass:Mass];
		[item release];
		
		
	}
	
	/// gan ket cau thanh cau toi 10 //(iNumberPiece-1) 1_1-> 1_10	
	for (int i=0; i<9; i++) 
	{
		
		
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+i)];
		cpCCSprite *image2=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+i+1)];
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:image2.shape->body toBodyAnchor:cpv(-5,-3) fromBodyAnchor:cpv(-5,-3)];//cpConstraint *pin1=
		cpConstraint *pin2=[_spaceManager addPinToBody:image1.shape->body fromBody:image2.shape->body toBodyAnchor:cpv(-5,3) fromBodyAnchor:cpv(-5,3)];//cpConstraint *pin2=
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		cpConstraintNode *P2 = [cpConstraintNode nodeWithConstraint:pin2];
		P1.color = ccBLACK;//ccRED
		P2.color = ccBLACK;//ccRED
		[self addChild:P1];
		[self addChild:P2];
		
		
		
		
	}
	/// noi khuc tren va duoi 1_1 vs 2_1 vs 3_1 vs 4_1
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+0)]; ///1_1
		cpCCSprite *image2=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+10)]; ///2_1		
		cpCCSprite *image3=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+20)]; ///3_1
		cpCCSprite *image4=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+23)]; ///4_1
		cpConstraint *pin1=[_spaceManager addSpringToBody:image1.shape->body fromBody:image2.shape->body stiffness:90000.0];//cpConstraint *pin1=			
		cpConstraint *pin2=[_spaceManager addSpringToBody:image2.shape->body fromBody:image3.shape->body stiffness:90000.0];		
		cpConstraint *pin3=[_spaceManager addSpringToBody:image3.shape->body fromBody:image4.shape->body stiffness:90000.0];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		cpConstraintNode *P2 = [cpConstraintNode nodeWithConstraint:pin2];
		cpConstraintNode *P3 = [cpConstraintNode nodeWithConstraint:pin3];
		P1.color = ccBLACK;//ccRED
		P2.color = ccBLACK;//ccRED
		P3.color = ccBLACK;//ccRED
		
		[self addChild:P1];
		[self addChild:P2];
		[self addChild:P3];
		
	}
	for (int i=10; i<19; i++)
	{
		
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+i)];
		cpCCSprite *image2=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+i+1)];
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:image2.shape->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		//[_spaceManager addPinToBody:image1.shape->body fromBody:image2.shape->body toBodyAnchor:cpv(5,-4) fromBodyAnchor:cpv(-4,-7)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
		
	}	
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+21)];
		cpCCSprite *image2=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+22)];
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:image2.shape->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];//cpConstraint *pin1=
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
	}
	//1_10 vs 2_10 vs 3_10 vs 4_10 vs 
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+9)];  //1_10
		cpCCSprite *image2=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+19)];   //2_10
		cpCCSprite *image3=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+22)];   //3_10
		
		
		
		cpConstraint *pin1=[_spaceManager addSpringToBody:image1.shape->body fromBody:image2.shape->body stiffness:90000.0];//cpConstraint *pin1=			
		cpConstraint *pin2=[_spaceManager addSpringToBody:image2.shape->body fromBody:image3.shape->body stiffness:90000.0];		
		
		
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		cpConstraintNode *P2 = [cpConstraintNode nodeWithConstraint:pin2];	
		
		P1.color = ccBLACK;//ccRED
		P2.color = ccBLACK;//ccRED
		
		
		[self addChild:P1];
		[self addChild:P2];
		
		
	}
	///3_1 vd lineLeft1
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+20)];		
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:lineLeft1->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
	}
	//2_10 vs lineLeft2
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+10)];			
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:lineLeft2->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
		
	}
	///1_1 vs lineleft3 
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+0)];			
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:lineLeft3->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
	}
	/// 3_10 vs lineRight1
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+22)];		
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:lineRight1->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
		
	}
	//2_10 vs lineRight2
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+19)];		
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:lineRight2->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
	}
	//1_10 vs linerignt3
	{
		cpCCSprite *image1=(cpCCSprite *)[self getChildByTag:(ktagImageBridge+9)];		
		
		cpConstraint *pin1=[_spaceManager addPinToBody:image1.shape->body fromBody:lineRight3->body toBodyAnchor:cpv(0,0) fromBodyAnchor:cpv(0,0)];
		cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		P1.color = ccBLACK;//ccRED
		[self addChild:P1];
	}
	
}

-(cpShape *)setupWallbyColor:(int)_color andColition:(int)kColition  from:(cpVect)P1 to:(cpVect)P2 
{
	cpShape *_item;
	_item= [_spaceManager addSegmentAtWorldAnchor:P1 toWorldAnchor:P2 mass:STATIC_MASS radius:0];
	_item->collision_type = kColition;
	[self drawLineWall:_item:_color];
	return _item;
}
-(void)drawLineWall:(cpShape *)_object:(int)_color
{
	
	cpShapeNode *W2 = [cpShapeNode nodeWithShape:_object];
	if(_color==1)
	{
		W2.color = ccORANGE;
	}
	else if(_color==2)
	{
		W2.color = ccYELLOW;
	}
	else if(_color==3)
	{
		W2.color = ccWHITE;
	}
	else if(_color==4)
	{
		W2.color = ccBLUE;
	}
	else if(_color==5)
	{
		W2.color = ccBLACK;
	}
	else {
		W2.color=ccWHITE;
	}

	
	[self addChild:W2];
}


-(void)drawline:(cpShape *)_object
{
	cpShapeNode *W2 = [cpShapeNode nodeWithShape:_object];
	W2.color = ccORANGE;
	[self addChild:W2];
}
















-(void)forTestingNotUse
{
	{
		/*cpShape *Wheel1=[self createImgePillarsAt:ccp(0+32*2,120+96) width:32 height:(32-12):@"trai1.png"];
		 cpShape *Wheel2=[self createImgePillarsAt:ccp(0+32*3,120+96) width:32 height:(32-12):@"trai1.png"];
		 cpConstraint *pin1=[_spaceManager addPinToBody:Wheel1->body fromBody:Wheel2->body toBodyAnchor:cpv(-5,4) fromBodyAnchor:cpv(4,7)];
		 cpConstraint *pin2=[_spaceManager addPinToBody:Wheel1->body fromBody:Wheel2->body toBodyAnchor:cpv(-5,-4) fromBodyAnchor:cpv(-4,-7)];
		 
		 cpShapeNode *W1 = [cpShapeNode nodeWithShape:Wheel1];
		 cpShapeNode *W2 = [cpShapeNode nodeWithShape:Wheel2];
		 cpConstraintNode *P1 = [cpConstraintNode nodeWithConstraint:pin1];
		 cpConstraintNode *P2 = [cpConstraintNode nodeWithConstraint:pin2];
		 
		 W1.color = ccBLUE;
		 W2.color = ccORANGE;
		 P1.color = ccRED;
		 P2.color = ccRED;
		 
		 [self addChild:W1];
		 [self addChild:W2];
		 [self addChild:P1];
		 [self addChild:P2];*/
		
	}
}


//creation




- (CCNode<cpCCNodeProtocol>*) createTriangleAt:(cpVect)pt size:(int)size mass:(int)mass:(int)kBlockCollisionType
{
	CGPoint d1 = ccpForAngle(CC_DEGREES_TO_RADIANS(330));
	CGPoint d2 = ccpForAngle(CC_DEGREES_TO_RADIANS(210));
	CGPoint d3 = ccpForAngle(CC_DEGREES_TO_RADIANS(90));
	
	d1 = ccpMult(d1, size);
	d2 = ccpMult(d2, size);
	d3 = ccpMult(d3, size);
	
	cpShape *shape = [_spaceManager addPolyAt:pt mass:mass rotation:0 numPoints:3 points:d1,d2,d3,nil];
	shape->collision_type = kBlockCollisionType;
	
	cpShapeNode *node = [cpShapeNode nodeWithShape:shape];
	node.color = ccc3(100, 20, 40);
	
	
	[self addChild:node];
	
	return node;
}
- (cpShape*) createImageBlockAt:(cpVect)pt width:(int)w height:(int)h mass:(int)mass:(int)kBlockCollisionType:(NSString *)strName
{
	int tempKBlock=0;
	BOOL isCar=FALSE;
	if(kBlockCollisionType==kCar)
	{
		tempKBlock=ktagCar;
		isCar=TRUE;
	}
	else {
		tempKBlock=iNumberPiece;
	}

	
	cpShape *shape = [_spaceManager addRectAt:pt mass:mass width:w height:h rotation:0];
	shape->collision_type = kBlockCollisionType;
	
	cpCCSprite *image=[cpCCSprite spriteWithShape:shape file:strName];
	[self addChild:image z:1 tag:(ktagImageBridge+tempKBlock)];
	
	if(isCar==FALSE)
	{
		iNumberPiece++;
	}
	/*cpShapeNode *node = [cpShapeNode nodeWithShape:shape];
	 node.color = ccc3(100, 20, 40);
	 [self addChild:node];*/
	
	
	return shape;
	
}


-(cpShape *)createImageEntityAt:(cpVect)pos width:(int)w height:(int)h fileName:(NSString*)strName kColitionType:(int)kBlockCollisionType Mass:(int)_myMass
{
	cpShape *_shape=[self createImageBlockAt:cpvadd(pos, cpv(0,h)) width:w height:h mass:_myMass :kBlockCollisionType:strName];/// mass M
	
	return _shape;
	
	
	//pillars 1
	/*[self createBlockAt:cpvadd(pos, cpv(0,H1/2)) width:W1 height:H1 mass:M:1];
	 [self createBlockAt:cpvadd(pos, cpv(W2,H1/2)) width:W1 height:H1 mass:M:1];
	 [self createBlockAt:cpvadd(pos, cpv(W2*2,H1/2)) width:W1 height:H1 mass:M:1];
	 
	 //floor 1
	 [self createBlockAt:cpvadd(pos, cpv(W2/2-W1/4,H1+H2/2)) width:W2+W1/2 height:H2 mass:M:1];
	 [self createBlockAt:cpvadd(pos, cpv(W2+W2/2+W1/4,H1+H2/2)) width:W2+W1/2 height:H2 mass:M:1];*/
}


-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	
	return YES;
	
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	
	
	
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
	
}
-(void)dealloc
{
	[super dealloc];
	//[_saveStructBridge release];
}
@end
